<template>
  <div>
    <el-dialog v-model="addinfo.flag" :title="addinfo.isShow ? '添加菜单' : '编辑菜单'" center>
      <el-form :model="menuForm" :rules="rules" ref="formRef">
        <el-form-item prop="rolename" label="角色名称" :label-width="formLabelWidth">
          <el-input autocomplete="off" v-model="menuForm.rolename" />
        </el-form-item>
        <el-form-item label="角色权限" :label-width="formLabelWidth">
          <el-tree check-strictly ref="tree" :data="store.getters['menu/getMenu']" show-checkbox node-key="id"
            default-expand-all :props="{
              children: 'children',
              label: 'title',
            }">
          </el-tree>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch v-model="menuForm.status" style="
            --el-switch-on-color: #13ce66;
            --el-switch-off-color: #ff4949;
            " :active-value="1" :inactive-value="2" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button @click="confirm"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, defineProps, defineEmits, defineExpose } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from 'element-plus'
import { getaddrole, getroleedit, getroleinfo } from '../../request/api'
import { useStore } from 'vuex'
let store = useStore()
let tree = ref()
//页面一打开判断menu列表
let mounted = () => {
  if (store.getters['menu/getMenu'].length == 0) {
    store.dispatch('menu/changeMenuAction')
  }
}
mounted()


//接收父组件的属性
let props = defineProps({
  addinfo: Object,
});
let emit = defineEmits(["cancel"]);
//label宽度
let formLabelWidth = ref("120px");
let formRef = ref<FormInstance>();
//创建一个表单对象
let menuForm = ref({
  rolename: "",
  menus: "",
  status: 1,
});

//规则验证
let rules = reactive<FormRules>({
  rolename: [
    { required: true, message: "请输入菜单名称", trigger: "blur" },
    { min: 2, max: 6, message: "Length should be 2 to 6", trigger: "blur" },
  ],
});
//封装重置事件
let reset = () => {
  menuForm.value = ref({
    rolename: "",
    menus: "",
    status: 1,
  });
  //清空验证规则
  formRef.value.resetFields()
  tree.value.setCheckedKeys([])

};
//封装一个取消事件
let cancel = () => {
  emit("cancel", false);
  reset();
};

//封装一个获取一条数据的方法
let lookup = (id: any) => {
  getroleinfo({ id }).then((res: any) => {
    menuForm.value = res.list
    menuForm.value.id = id
    //将角色权限id转成数组
    menuForm.value.menus ? tree.value.setCheckedKeys(menuForm.value.menus.split(',')) : tree.value.setCheckedKeys([])
  })
  
}
defineExpose({
  lookup,
});

//封装一个确定事件
const confirm = () => {
  formRef.value?.validate((val) => {
    if (val) {
      menuForm.menus = tree.value.getCheckedKeys().join(',')
      if (props.addinfo.isShow) {
        getaddrole(menuForm.value).then((res: any) => {
          if (res.code == 200) {
            ElMessage.success(res.msg)
            store.dispatch('role/getroleActive')
            cancel()
          } else {
            ElMessage.error(res.msg)
          }
        })
      } else {
        //编辑接口
        getroleedit(menuForm.value).then((res: any) => {
          if (res.code == 200) {
            ElMessage.success(res.msg)
            store.dispatch('role/getroleActive')
            cancel()
          } else {
            ElMessage.error(res.msg)
          }
        })
      }

    }
  });
};
</script>

<style lang="less" scoped></style>
